package com.zhuoyue.biz.core.api.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhuoyue.biz.core.api.dao.FingerprintDao;
import com.zhuoyue.biz.core.api.domain.FingerprintDO;
import com.zhuoyue.biz.core.api.service.FingerprintService;
import com.zhuoyue.common.constant.ResultCode;
import com.zhuoyue.common.dto.BaseResultDTO;
import com.zhuoyue.common.dto.BatchResultDTO;
import com.zhuoyue.common.dto.ResultDTO;
/**
 * @ClassName: FingerprintServiceImpl
 * @Description:
 * @author wjming
 * @Date 2021-07-14 20:26
 */
@Service
public class FingerprintServiceImpl extends ServiceImpl<FingerprintDao, FingerprintDO> implements FingerprintService {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private FingerprintDao fingerprintDao;


    @Override
    public List<FingerprintDO> getFingerprintListByDeviceId(String deviceId) {
        List<FingerprintDO> list = fingerprintDao.selectFingerprintListByDeviceId(deviceId);
        if (CollectionUtils.isEmpty(list)) {
            throw new ZyServerException(ErrorType.result_not_exist.getCode(), "该指纹锁没有录入指纹信息!");
        }
        return list;
    }


    @Override
    public FingerprintDO addFingerprint(FingerprintDO fingerprint) {
        //判断编号是否已存在
        int i = fingerprintDao.selectCount(Wrappers.<FingerprintDO>lambdaQuery().eq(FingerprintDO::getDeviceId, fingerprint.getDeviceId())
                .eq(FingerprintDO::getNumber, fingerprint.getNumber()));
        if (i > 0) {
            throw new ZyServerException(ErrorType.Service_Fail.getCode(), "指纹编号已存在");
        }
        int count = fingerprintDao.insert(fingerprint);
        if (count < 1) {
            throw new ZyServerException(ErrorType.Service_Fail.getCode(), "指纹信息录入失败,请检查指纹锁设备是否存在");
        }

        FingerprintDO fingerprintDO = fingerprintDao.selectFingerprintById(fingerprint.getId());
        return fingerprintDO;
    }


    @Override
    public FingerprintDO updateFingerprintById(FingerprintDO fingerprint) {
        int count = fingerprintDao.updateFingerprintSelective(fingerprint);
        if (count < 1) {
            throw new ZyServerException(ErrorType.OBJECT_NOT_FOUND.getCode(), "指纹录入信息修改失败");
        }
        FingerprintDO fd = fingerprintDao.selectFingerprintById(fingerprint.getId());
        return fd;
    }


    @Override
    public BaseResultDTO deleteFingerprinById(String id) {
        BaseResultDTO result = new BaseResultDTO();

        try {
            int count = fingerprintDao.deleteFingerprinById(id);
            if (count < 1) {
                result.setSuccess(false);
                result.setResultCode(ResultCode.FAIL);
                result.setErrorDetail("指纹录入信息删除失败！");
                return result;
            }

            result.setSuccess(true);
            result.setResultCode(ResultCode.SUCCESS);
        } catch (Exception e) {
            log.error("FingerprintServiceImpl call deleteFingerprinById error ==== ", e);
            result.setSuccess(false);
            result.setResultCode(ResultCode.SYS_ERROR);
            result.setErrorDetail("指纹信息删除系统异常！");
        }
        return result;
    }


}
